package group32.server;

import group32.client.SearchService;
import group32.shared.SearchResult;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class SearchServiceImpl extends RemoteServiceServlet implements SearchService{
	public List<SearchResult> search(String keys, int userID) throws IllegalArgumentException {
		List<SearchResult> l = new ArrayList<SearchResult>();
		//if(this.getThreadLocalRequest().getSession().getAttribute(userID+"")!=null){	
		ResultSet rs=new Connect().searchIDs(keys);
			int i =0;
			try {
			while(rs.next()){
				
					int photoid=rs.getInt("photoID");
					SearchResult sr = new SearchResult();
					sr.photoid=photoid;
					System.out.println(photoid);
					ResultSet rs1=new Connect().searchInfo(photoid, userID);
					String url="", circlename=null;
					int photoowner=-1,visibility=-1;
					float score=0;
					if(PhotoServiceImpl.scoreTable.get(photoid) != null)score =PhotoServiceImpl.scoreTable.get(photoid);
					System.out.println(score);
					while(rs1.next()){	
					url= rs1.getString("url");
					//circlename=rs1.getString("name");
					photoowner = rs1.getInt("photoOwner");
					sr.photoOwner= photoowner;
					sr.score= score;
					sr.url= url;
					if(visibility==-1)continue;
					sr.circleName= circlename;
					}
					l.add(sr);
				} 
				}catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		Collections.sort(l);
		return l;
	}

}
